<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Maximize stopband attenuation of a linear phase lowpass FIR filter</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/filter_design/html/fir_lin_phase_lowpass_max_atten.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Maximize stopband attenuation of a linear phase lowpass FIR filter</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% "Filter design" lecture notes (EE364) by S. Boyd</span>
<span class="comment">% (figures are generated)</span>
<span class="comment">%</span>
<span class="comment">% Designs a linear phase FIR lowpass filter such that it:</span>
<span class="comment">% - minimizes maximum stopband attenuation</span>
<span class="comment">% - has a constraint on the maximum passband ripple</span>
<span class="comment">%</span>
<span class="comment">% This is a convex problem (when sampled it can be represented as an LP).</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max |H(w)|                     for w in the stopband</span>
<span class="comment">%       s.t.   1/delta &lt;= |H(w)| &lt;= delta     for w in the passband</span>
<span class="comment">%</span>
<span class="comment">% where H is the frequency response function and variable is</span>
<span class="comment">% h (the filter impulse response). delta is allowed passband ripple.</span>
<span class="comment">%</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/02/06</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% user's filter specifications</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% filter order is 2n+1 (symmetric around the half-point)</span>
n = 10;

wpass = 0.12*pi;        <span class="comment">% passband cutoff freq (in radians)</span>
wstop = 0.24*pi;        <span class="comment">% stopband start freq (in radians)</span>
ripple = 1;    <span class="comment">% (delta) max allowed passband ripple in dB</span>
                        <span class="comment">% ideal passband gain is 0 dB</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% create optimization parameters</span>
<span class="comment">%********************************************************************</span>
N = 30*n;                              <span class="comment">% freq samples (rule-of-thumb)</span>
w = linspace(0,pi,N);
A = [ones(N,1) 2*cos(kron(w',[1:n]))]; <span class="comment">% matrix of cosines</span>

<span class="comment">% passband 0 &lt;= w &lt;= w_pass</span>
ind = find((0 &lt;= w) &amp; (w &lt;= wpass));    <span class="comment">% passband</span>
Ap  = A(ind,:);

<span class="comment">% transition band is not constrained (w_pass &lt;= w &lt;= w_stop)</span>

<span class="comment">% stopband (w_stop &lt;= w)</span>
ind = find((wstop &lt;= w) &amp; (w &lt;= pi));   <span class="comment">% stopband</span>
As  = A(ind,:);

<span class="comment">%********************************************************************</span>
<span class="comment">% optimization</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% formulate and solve the linear-phase lowpass filter design</span>
cvx_begin
  variable <span class="string">h(n+1,1)</span>;
  minimize(norm(As*h,Inf))
  subject <span class="string">to</span>
    10^(-ripple/20) &lt;= Ap*h &lt;= 10^(ripple/20);
cvx_end

<span class="comment">% check if problem was successfully solved</span>
disp([<span class="string">'Problem is '</span> cvx_status])
<span class="keyword">if</span> ~strfind(cvx_status,<span class="string">'Solved'</span>)
  <span class="keyword">return</span>
<span class="keyword">else</span>
  fprintf(1,<span class="string">'The minimum attenuation in the stopband is %3.2f dB.\n\n'</span>,<span class="keyword">...</span>
          20*log10(cvx_optval));
  <span class="comment">% construct the full impulse response</span>
  h = [flipud(h(2:end)); h];
<span class="keyword">end</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% plots</span>
<span class="comment">%********************************************************************</span>
figure(1)
<span class="comment">% FIR impulse response</span>
plot(-n:n,h',<span class="string">'o'</span>,[-n:n;-n:n],[zeros(1,2*n+1);h'],<span class="string">'b:'</span>,[-n-1,n+1],[0,0],<span class="string">'k-'</span>);
xlabel(<span class="string">'t'</span>), ylabel(<span class="string">'h(t)'</span>)
set(gca,<span class="string">'XLim'</span>,[-n-1,n+1])

figure(2)
<span class="comment">% frequency response</span>
H = exp(-j*kron(w',[0:2*n]))*h;
<span class="comment">% magnitude</span>
subplot(2,1,1)
plot(w,20*log10(abs(H)),<span class="keyword">...</span>
     [0 wpass],[ripple ripple],<span class="string">'r--'</span>,<span class="keyword">...</span>
     [0 wpass],[-ripple -ripple],<span class="string">'r--'</span>);
axis([0,pi,-50,10])
xlabel(<span class="string">'w'</span>), ylabel(<span class="string">'mag H(w) in dB'</span>)
<span class="comment">% phase</span>
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel(<span class="string">'w'</span>), ylabel(<span class="string">'phase H(w)'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling Mosek 9.1.9: 756 variables, 240 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 240             
  Cones                  : 228             
  Scalar variables       : 756             
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 240             
  Cones                  : 228             
  Scalar variables       : 756             
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 12
Optimizer  - Cones                  : 228
Optimizer  - Scalar variables       : 528               conic                  : 456             
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 78                after factor           : 78              
Factor     - dense dim.             : 0                 flops                  : 7.20e+04        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   2.3e+02  1.1e+00  1.0e+00  0.00e+00   0.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   1.6e+02  7.8e-01  5.6e-01  9.16e+00   -5.795077210e-01  -4.726720519e-02  7.0e-01  0.01  
2   3.0e+01  1.5e-01  1.9e-02  1.83e+00   -8.741300310e-02  -8.133214956e-02  1.3e-01  0.01  
3   2.0e+01  1.0e-01  8.5e-03  2.90e+00   -4.012749313e-02  -3.704675033e-02  9.0e-02  0.01  
4   1.2e+01  6.1e-02  3.6e-03  1.90e+00   -2.502064188e-02  -2.352422328e-02  5.4e-02  0.01  
5   6.3e+00  3.1e-02  1.2e-03  1.35e+00   -2.106833753e-02  -2.052353467e-02  2.8e-02  0.01  
6   1.6e+00  7.8e-03  1.3e-04  1.18e+00   -1.773044194e-02  -1.773278508e-02  7.0e-03  0.01  
7   5.5e-01  2.7e-03  2.2e-05  1.03e+00   -1.751452393e-02  -1.755166447e-02  2.4e-03  0.01  
8   9.9e-02  4.9e-04  1.3e-06  1.01e+00   -1.745869549e-02  -1.747115767e-02  4.4e-04  0.01  
9   2.8e-02  1.4e-04  1.5e-07  1.00e+00   -1.747206379e-02  -1.747652802e-02  1.2e-04  0.02  
10  4.3e-03  2.1e-05  8.6e-09  1.00e+00   -1.747527281e-02  -1.747596758e-02  1.9e-05  0.02  
11  1.5e-04  7.6e-07  4.4e-11  1.00e+00   -1.747615732e-02  -1.747618518e-02  6.7e-07  0.02  
12  3.1e-07  1.5e-09  3.9e-15  1.00e+00   -1.747619656e-02  -1.747619661e-02  1.3e-09  0.02  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -1.7476196558e-02   nrm: 1e+00    Viol.  con: 1e-08    var: 5e-11    cones: 0e+00  
  Dual.    obj: -1.7476196613e-02   nrm: 2e-01    Viol.  con: 0e+00    var: 7e-11    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 12        time: 0.02    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0174762
 
Problem is Solved
The minimum attenuation in the stopband is -35.15 dB.

</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="fir_lin_phase_lowpass_max_atten__01.png" alt=""> <img src="fir_lin_phase_lowpass_max_atten__02.png" alt=""> 
</div>
</div>
</body>
</html>